package com.scs.application.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Description：HikariPool 数据源配置
 */
@Component
public class HikariPoolManager {
    private static Logger logger = LoggerFactory.getLogger(HikariPoolManager.class);


    // 数据库服务器addr
    private static String db_url = null;
    // 数据库登录用户名
    private static String db_username = null;
    // 数据库登录密码
    private static String db_password = null;
    // 数据库驱动
    private static String db_driverClassName = null;
    // 数据库连接数
    private static short db_max_conn = 0;
    // 数据库连接端口
    private static short db_port = 0;
    // 数据库名称
    private static String db_name = null;

    private static HikariDataSource dataSource;
    private static HikariPoolManager hikariPoolManager;

    /**
     * 单例模式
     *
     * @return
     * @param url
     * @param driverClassName
     * @param username
     * @param password
     */
    public static HikariPoolManager getInstance(String url, String driverClassName, String username, String password) {
        if (hikariPoolManager == null){
            db_url=url;
            db_driverClassName=driverClassName;
            db_username=username;
            db_password=password;
            hikariPoolManager = new HikariPoolManager();
        }
        return hikariPoolManager;
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static HikariDataSource getdDataSource() {
        return dataSource;
    }

    /**
     * 启动
     *
     * @throws IOException
     * @throws SQLException
     */
    public  static void start() throws IOException, SQLException {
        logger.info("连接池初始化Start!!!");
        if (db_url == null || db_url.length() == 0) {
            logger.error("配置的数据库ip地址错误!");
            System.exit(0);
        }

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(db_url);
        config.setUsername(db_username);
        config.setPassword(db_password);
        config.setDriverClassName(db_driverClassName);
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.addDataSourceProperty("useInformationSchema", "true");
        // 设置连接超时为8小时
        config.setConnectionTimeout(8 * 60 * 60);
        dataSource = new HikariDataSource(config);

        logger.info("连接池初始化End!!!");
    }

    /**
     * 打开连接
     *
     * @return
     * @throws SQLException
     */
    public synchronized static Connection getConnection() throws SQLException {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            logger.error("error:" + e.getMessage());
            dataSource.close();
            return null;
        }
    }

    /**
     * 关闭连接
     *
     * @return
     * @throws SQLException
     */
    public static boolean stop() throws SQLException {
        dataSource.close();
        return true;
    }
}
